import 'package:flutter/material.dart';

/// 自定义横向虚线类
class DashedHorizontalLine extends StatelessWidget {
  /// 虚线的宽度
  final double width;
  /// 虚线的高度
  final double height;
  /// 虚线的颜色
  final Color color;
  /// 每段虚线的长度
  final double dashLength;
  /// 虚线之间的间距
  final double dashGap;

  const DashedHorizontalLine({
    super.key,
    this.width = double.infinity,
    this.height = 1.0,
    this.color = const Color(0xFFededed),
    this.dashLength = 10.0,
    this.dashGap = 5.0,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(width, height),
      painter: _DashedLinePainter(
        color: color,
        dashLength: dashLength,
        dashGap: dashGap,
      ),
    );
  }
}

/// 自定义绘制虚线的 Painter
class _DashedLinePainter extends CustomPainter {
  final Color color;
  final double dashLength;
  final double dashGap;

  _DashedLinePainter({
    required this.color,
    required this.dashLength,
    required this.dashGap,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = size.height
      ..strokeCap = StrokeCap.round;

    double startX = 0;
    while (startX < size.width) {
      canvas.drawLine(
        Offset(startX, size.height / 2),
        Offset(startX + dashLength, size.height / 2),
        paint,
      );
      startX += dashLength + dashGap;
    }
  }

  @override
  bool shouldRepaint(covariant _DashedLinePainter oldDelegate) {
    return oldDelegate.color != color ||
        oldDelegate.dashLength != dashLength ||
        oldDelegate.dashGap != dashGap;
  }
}